//
//  WBWebBar.m
//  WeatherBar
//
//  Created by Bert on 2024/6/17.
//

#import "WBWebBar.h"
#import "UIImage+WBImage.h"

@interface WBWebBar ()

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic ,strong) UIView *backView;

@property (nonatomic, assign) BOOL titleHidden;

@end

@implementation WBWebBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _var_backClickSubject = [RACSubject subject];
        _var_closeClickSubject = [RACSubject subject];
        _var_rightClickSubject = [RACSubject subject];
        [self wb_setupSubViews];
    }
    return self;
}

- (void)wb_setupSubViews
{
    _backgroundView = [UIView new];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    _backgroundView.alpha = 0;
    [self addSubview:_backgroundView];
    
    _var_titleLabel = [UILabel new];
    _var_titleLabel.text = @"";
    _var_titleLabel.textAlignment = NSTextAlignmentCenter;
    _var_titleLabel.textColor = [UIColor whiteColor];
    _var_titleLabel.textAlignment = NSTextAlignmentCenter;
    _var_titleLabel.font = kBoldFontSize(16);
    [self addSubview:_var_titleLabel];
    
    _backView = [[UIView alloc] init];
    _backView.backgroundColor = [UIColor clearColor];
    _backView.userInteractionEnabled = YES;
    [self addSubview:_backView];
    
    _var_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _var_backButton.frame = CGRectMake(7, 7, 24, 24);//(7, 7, 9, 16);
    UIImage *backImage = [UIImage imageNamed:@"icon_navBack"];
//    CGSize imageSize = [backImage th_imageSizeByTargetWidth:18];
    [_var_backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    _var_backButton.userInteractionEnabled = NO;
    [_backView addSubview:_var_backButton];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    @weakify(self);
    [tap.rac_gestureSignal subscribeNext:^(NSNumber *x) {
        @strongify(self);
        [self.var_backClickSubject sendNext:nil];
    }];
    [_backView addGestureRecognizer:tap];

    
    _var_rightShareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_var_rightShareButton];
    _var_rightShareButton.frame = CGRectMake(7, 7, 18, 18);//(7, 7, 9, 16);
    _var_rightShareButton.userInteractionEnabled = YES;
    _var_rightShareButton.hidden = YES;
    [_var_rightShareButton setBackgroundImage:[UIImage imageNamed:@"top_rigwb_share_white"] forState:UIControlStateNormal];
    
    _var_closeView = [[UIView alloc] init];
    _var_closeView.backgroundColor = [UIColor clearColor];
    _var_closeView.userInteractionEnabled = YES;
    UIImageView *closeBtn = [[UIImageView alloc] initWithFrame:CGRectMake(7.5,7.5, 15, 15)];
    closeBtn.image = [UIImage imageNamed:@"webview_close"];
    [_var_closeView addSubview:closeBtn];
    [self addSubview:self.var_closeView];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] init];
    [tap1.rac_gestureSignal subscribeNext:^(NSNumber *x) {
        @strongify(self);
        [self.var_closeClickSubject sendNext:nil];
    }];
    [_var_closeView addGestureRecognizer:tap1];
//    _closeView.hidden = YES;
    
    _var_moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_var_moreButton setBackgroundImage:[UIImage imageNamed:@"commerce_more_top"] forState:UIControlStateNormal];
    [self addSubview:_var_moreButton];
    
    _var_lineView = [UIView new];
    _var_lineView.backgroundColor = [UIColor colorDDD];
    [self.backgroundView addSubview:_var_lineView];
    
    _var_rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _var_rightBtn.frame = CGRectMake(kScreenWidth-50, self.wb_bottom-38, 35, 35);
    [_var_rightBtn setImage:[UIImage imageNamed:@"webview_close"] forState:(UIControlStateNormal)];
//    [_rightBtn addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    _var_rightBtn.hidden = YES;
    [self addSubview:_var_rightBtn];
    [[_var_rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        [self.var_rightClickSubject sendNext:nil];
    }];
    
    _var_moreButton.hidden = YES;
    _var_rightBtn.hidden = YES;
}

- (void)wb_changeBackgroundViewAlpha:(CGFloat)value
{
    if (value>=0.2) {
//        [_attentionBtn setImage:[UIImage imageNamed:@"myclub_attentionNav_sel"] forState:UIControlStateSelected];
//        [_attentionBtn setImage:[UIImage imageNamed:@"myclub_attentionNav"] forState:UIControlStateNormal];
//        _attentionBtn.selected = NO;
        [_var_backButton setBackgroundImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
        _var_backButton.wb_size = CGSizeMake(24, 24);
//        [_rightShareButton setBackgroundImage:[UIImage imageNamed:@"top_rigwb_share_black"] forState:UIControlStateNormal];
//        _rightShareButton.size = CGSizeMake(24, 24);
        _var_titleLabel.textColor = [UIColor blackColor];
        _var_titleLabel.hidden = NO;
    }else{
//        [_attentionBtn setImage:[UIImage imageNamed:@"myclub_attention_sel"] forState:UIControlStateSelected];
//        [_attentionBtn setImage:[UIImage imageNamed:@"myclub_attention"] forState:UIControlStateNormal];
//        _attentionBtn.selected = YES;
        [_var_backButton setBackgroundImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
        _var_backButton.wb_size = CGSizeMake(24, 24);
//        [_rightShareButton setBackgroundImage:[UIImage imageNamed:@"top_rigwb_share_white"] forState:UIControlStateNormal];
//        _rightShareButton.size = CGSizeMake(24, 24);
        _var_titleLabel.textColor = [UIColor whiteColor];
        _var_titleLabel.hidden = self.titleHidden;
    }
    _backgroundView.alpha = value;
}
- (void)wb_hideCloseBtn
{
    self.var_closeView.hidden = YES;
}
- (void)wb_hiddenTitle
{
    self.titleHidden = YES;
    self.var_titleLabel.hidden = YES;
}
- (CGFloat)wb_backgroundViewAlpha
{
    return self.backgroundView.alpha;
}
- (void)wb_setBackArrowBlack
{
    [_var_backButton setBackgroundImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
    _backgroundView.alpha = 1.0;
}
- (void)wb_setAttentionBtn
{
    [self addSubview:self.var_attentionBtn];
    [self.var_attentionBtn setFrame:CGRectMake(kScreenWidth-39, self.backView.wb_y, 24, 24)];
    self.var_attentionBtn.wb_y = self.wb_height - 30;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _backgroundView.frame = self.bounds;
    
    self.backView.wb_width = 35;
    self.backView.wb_height = 35;
    self.backView.wb_left = 7.5;
    self.backView.wb_bottom = self.wb_height - 3;
    
    self.var_rightShareButton.wb_right = self.bounds.size.width - 15;
    self.var_rightShareButton.wb_centerY = self.backView.wb_centerY;
    
    self.var_closeView.wb_width = 35;
    self.var_closeView.wb_height = 35;
    self.var_closeView.wb_left = self.backView.wb_right+7.5;
    self.var_closeView.wb_bottom = self.wb_height - 3;
    
    [self.var_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.var_closeView.mas_right);
        //make.right.mas_equalTo(-80);  //与centerX约束冲突
        make.centerY.mas_equalTo(self.var_backButton);
        make.centerX.mas_equalTo(self.backgroundView);
    }];
    
    self.var_moreButton.wb_right = self.wb_width - 10;
    self.var_moreButton.wb_bottom = self.wb_height - 6;
    self.var_moreButton.wb_width = 32;
    self.var_moreButton.wb_height = 32;
    
    [self.var_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-SINGLE_LINE_ADJUST_OFFSET);
        make.height.mas_equalTo(SINGLE_LINE_WIDTH);
    }];
}
#pragma mark lazyloading
- (UIButton *)var_attentionBtn
{
    if (_var_attentionBtn == nil) {
        _var_attentionBtn = [[UIButton alloc] init];
        [_var_attentionBtn setImage:[UIImage imageNamed:@"myclub_attention_sel"] forState:UIControlStateSelected];
        [_var_attentionBtn setImage:[UIImage imageNamed:@"myclub_attention"] forState:UIControlStateNormal];
        @weakify(self);
        _var_attentionSubject = [RACSubject subject];
        [[_var_attentionBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self.var_attentionSubject sendNext:self.var_attentionBtn];
        }];
        _var_attentionBtn.hidden = YES;
    }
    return _var_attentionBtn;
}
@end
